const Vue  = require('vue'),
	Vuex   = require('vuex'),
	axios  = require('axios').create(),
	format = require('./filter/formatDeep.js')


Vue.use(Vuex)

axios.defaults.transformResponse.push(data => format(data))

module.exports = new Vuex.Store({
	state: {
		newsList: [],
		news: {},
	},
	mutations: {
		getNewsList (state, list) {
			state.newsList = list
		},
		getNews (state, news) {
			state.news = news

		}
	},
	actions: {
		getNewsList (context, page) {
			context.commit('getNewsList', [])
			return axios.post(
				'https://api2.alltennis.cn/api/2/topic/gets',
				{
					parentId: "",
					groupId : "5799c33b4bde26cfeb600984",
					rootId  : "",
					page    : page.toString(),
				}
			)
			.then(
				(response) => {
					context.commit('getNewsList', response.data.datalist)
				}
			)
		},
		getNews (context, id) {
			context.commit('getNews', {})
			return axios.post(
				'https://api2.alltennis.cn/api/2/news/getAll',
				{
					id: id,
				}
			)
			.then(
				(response) => {
					context.commit('getNews', response.data.datalist[0])
				}
			)
		},
	}
})
